package com.module.third;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.ExifInterface;
import android.media.MediaMetadataRetriever;
import android.util.Pair;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.module.frame.converter.MyTypeAdapterFactory;

import java.io.ByteArrayOutputStream;
import java.lang.reflect.Type;
import java.util.Map;

public class ThirdUtil {

    public static Gson getGson() {
        Gson gson = new GsonBuilder().registerTypeAdapterFactory(new MyTypeAdapterFactory<>()).create();
        return gson;
    }

    /**
     * Gosn解析，会自动处理数据类型不一样等问题
     */
    public static <T> T fromJson(String json, Type type) {
        return getGson().fromJson(json, type);
    }

    public static String toJson(Object o) {
        return getGson().toJson(o);
    }


    /**
     * 根据json字符串返回Map对象
     *
     * @param json
     * @return
     */
    public static Map<String, Object> toMap(String json) {
        return ThirdUtil.fromJson(json, new TypeToken<Map<String, Object>>() {
        }.getType());
    }


    /**
     * 压缩图片
     *
     * @param bitmap    被压缩的图片
     * @param sizeLimit 大小限制
     * @return 压缩后的图片
     */
    public static byte[] compressBitmap(Bitmap bitmap, float sizeLimit) {
        if (bitmap == null) {
            return null;
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int quality = 100;
        bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos);

        // 循环判断压缩后图片是否超过限制大小
        while (baos.toByteArray().length / 1024 > sizeLimit) {
            // 清空baos
            baos.reset();
            bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos);
            quality -= 5;
        }

//        Bitmap newBitmap = BitmapFactory.decodeStream(new ByteArrayInputStream(baos.toByteArray()), null, null);

        return baos.toByteArray();
    }


    /**
     * 根据路径获取对应的宽高
     */
    public static Pair<Integer,Integer> getWidth2Height(String path, boolean isVideo) {
        Pair<Integer,Integer> pair = null;
        try {
            //一定要先设置文件类型，才有用
            if (isVideo) {
                //取第一帧获取宽高，不要用MediaMetadataRetriever直接获取，不准
                MediaMetadataRetriever retr = new MediaMetadataRetriever();
                retr.setDataSource(path);
                Bitmap bm = retr.getFrameAtTime();
                int width = bm != null ? bm.getWidth() : 0;
                int height = bm != null ? bm.getHeight() : 0;
                pair = new Pair<>(width,height);
                if (bm != null && !bm.isRecycled()) {
                    bm.recycle();
                    bm = null;
                }
                retr.release();
            } else {
                //获取Options对象
                BitmapFactory.Options options = new BitmapFactory.Options();
                //仅做解码处理，不加载到内存
                options.inJustDecodeBounds = true;
                //解析文件
                BitmapFactory.decodeFile(path, options);
                int imageWidth = options.outWidth;
                int imageHeight = options.outHeight;

                ExifInterface exif = new ExifInterface(path); // 用图像文件的路径初始化 ExifInterface
                int imageOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
                if (imageOrientation == ExifInterface.ORIENTATION_ROTATE_90 || imageOrientation == ExifInterface.ORIENTATION_ROTATE_270) {
                    // 如果图像的方向需要旋转 90 度或 270 度，交换宽度和高度
                    int temp = imageWidth;
                    imageWidth = imageHeight;
                    imageHeight = temp;
                }
                //获取宽高
                pair = new Pair<>(imageWidth,imageHeight);
                options = null;
            }
        } catch (Exception e) {
        }
        return pair;
    }
}
